package luongvd.weather.model.adapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import luongvd.weather.R;
import luongvd.weather.common.Utils;
import luongvd.weather.common.service.utils.NetworkUtil;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class AdapterAutocompleteNameCity extends ArrayAdapter<Address> implements Filterable {

	Context context;
	private Geocoder mGeocoder;
	private StringBuilder mSb;
	LayoutInflater inflater;
	
	
	public AdapterAutocompleteNameCity(Context context) {
		super(context, R.layout.item_list_name_city);
		this.context = context;
		mSb = new StringBuilder();
		mGeocoder = new Geocoder(context);
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	class ViewHolder {
		TextView name_city;
	}

	@Override
	public Filter getFilter() {
		Filter myFilter = new Filter() {
			@Override
			protected FilterResults performFiltering(
					final CharSequence constraint) {
				List<Address> addressList = new ArrayList<Address>();
				if(NetworkUtil.checkNetWork(context))
				{
					if (constraint != null) {
						try {
							addressList = mGeocoder.getFromLocationName((String) constraint+"Viet Nam", 10);
						} catch (IOException e) {
						}
					}
				}
				final FilterResults filterResults = new FilterResults();
				filterResults.values = addressList;
				filterResults.count = addressList.size();
				return filterResults;
			}

			@SuppressWarnings("unchecked")
			@Override
			protected void publishResults(final CharSequence contraint,
					final FilterResults results) {
				clear();
				if (results.count>0) {
					for (Address address : (List<Address>) results.values) {
						if(Utils.createFormattedAddressFromAddress(address,mSb).equals("")==false)
						{
							add(address);
						}
					}
					if (results.count > 0) {
						notifyDataSetChanged();
					}
				} else {
					notifyDataSetInvalidated();
				}
			}

			@Override
			public CharSequence convertResultToString(final Object resultValue) {
				return resultValue == null ? "" : Utils.createFormattedAddressFromAddress(
								(Address) resultValue, mSb);
			}
		};
		return myFilter;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_list_name_city, null);
			holder = new ViewHolder();
			holder.name_city = (TextView) convertView.findViewById(R.id.name_city);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		String txtAddress = Utils.createFormattedAddressFromAddress(getItem(position), mSb);
		holder.name_city.setText(txtAddress);
		return convertView;
	}
	
	

}
